package nongchang.kuaijiecaifu.com.ncapp_android.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import butterknife.BindView;
import butterknife.ButterKnife;
import nongchang.kuaijiecaifu.com.ncapp_android.R;
import nongchang.kuaijiecaifu.com.ncapp_android.common.adapter.ItemAdapter;
import nongchang.kuaijiecaifu.com.ncapp_android.retrofit.good.HomeItemCart;
import nongchang.kuaijiecaifu.com.ncapp_android.ui.BasketDetailActivity;
import nongchang.kuaijiecaifu.com.ncapp_android.ui.BasketMultipleDetailActivity;

/**
 * Created by Administrator on 2016/5/4 0004.
 */
public class BasketMultipleCartAdapter extends ItemAdapter<HomeItemCart, BasketMultipleCartAdapter.BasketCartViewHolder> {
    private Context context;

    public BasketMultipleCartAdapter(Context context) {
        super(context);
        this.context = context;
    }

    private View.OnClickListener onAddNum;  //加商品数量接口
    private View.OnClickListener onSubNum;  //减商品数量接口
//    private View.OnClickListener onDelete;  //删除数量
    private View.OnFocusChangeListener onTotalChange;
    private TextView.OnEditorActionListener onTotalTouch;

    public void setOnAddNum(View.OnClickListener onAddNum) {
        this.onAddNum = onAddNum;
    }

//    public void setOnDeleteNum(View.OnClickListener onDelete) {
//        this.onDelete = onDelete;
//    }

    public void setOnTotalNum(View.OnFocusChangeListener onTotalNum, TextView.OnEditorActionListener onTotalTouch) {
        this.onTotalChange = onTotalNum;
        this.onTotalTouch = onTotalTouch;
    }

    public void setOnSubNum(View.OnClickListener onSubNum) {
        this.onSubNum = onSubNum;
    }


    @Override
    public BasketCartViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new BasketCartViewHolder(inflate(R.layout.item_basket_cart, parent));
    }

    @Override
    public void onBindViewHolder(BasketCartViewHolder holder, int position) {
        HomeItemCart item = getItem(position);

            holder.relativeLayout.setVisibility(View.VISIBLE);
            holder.hotRelativeLayout.setVisibility(View.GONE);
            Glide.with(context).load(item.image).into(holder.imgCart);
            holder.txtTitle.setText(item.title);
            holder.txtOncePrice.setText("￥" + item.one_price);
            holder.btnSub.setOnClickListener(onSubNum);
            holder.btnAdd.setOnClickListener(onAddNum);
//        holder.btnDelete.setOnClickListener(onDelete);
            holder.btnSub.setTag(position);
            holder.btnAdd.setTag(position);
//        holder.btnDelete.setTag(position);
            holder.editText.setTag(position);
            holder.editText.setOnFocusChangeListener(onTotalChange);
            holder.editText.setOnEditorActionListener(onTotalTouch);
            holder.txtTotal.setText("共￥" + item.total_money);
            holder.editText.setText(item.num + "");
            holder.imgCart.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(v.getContext(),BasketDetailActivity.class);
                    intent.putExtra("hotType",item.hotType);
                    intent.putExtra("money",item.one_price);
                    v.getContext().startActivity(intent);
                }
            });
    }

    static class BasketCartViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.img_cart)
        ImageView imgCart;
        @BindView(R.id.img_icon)
        ImageView imgIcon;
        @BindView(R.id.txt_title)
        TextView txtTitle;

        @BindView(R.id.txt_once_price)
        TextView txtOncePrice;

        @BindView(R.id.txt_total)
        TextView txtTotal;

        @BindView(R.id.btn_sub)
        ImageView btnSub;

        @BindView(R.id.btn_add)
        ImageView btnAdd;

        @BindView(R.id.edit_total)
        EditText editText;

        @BindView(R.id.rl_layout)
        RelativeLayout relativeLayout;
        @BindView(R.id.rl_hot_layout)
        RelativeLayout hotRelativeLayout;

        @BindView(R.id.txt_hot_title)
        TextView txtHotTitle;

        @BindView(R.id.txt_hot_once_price)
        TextView txt_hot_once_price;
        @BindView(R.id.txt_hot_total)
        TextView txt_hot_total;
        @BindView(R.id.btn_hot_check)
        TextView btn_hot_check;
        public BasketCartViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
